package com.exam.loginByServlet;

import java.sql.*;

public class JDBCTest {

    public static boolean checkUser(String username, String password) throws ClassNotFoundException, SQLException {
        //配置信息
        String jdbcurl = "jdbc:mysql://localhost:3306/loginbyservlet?useUnicode=true&characterEncoding=utf-8&useSSL=true";
        String jdbcusername = "root";
        String jdbcpassword = "123456";
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.连接数据库，代表数据库
        Connection connection = DriverManager.getConnection(jdbcurl, jdbcusername, jdbcpassword);
        //3.向数据库发送SQL对象Statement:CRUD
        Statement statement = connection.createStatement();
        //4.编写sql语句
        String sql = "select * from userlogin where username = '" + username + "' and password = '" + password + "'";
        //5.执行查询SQL，返回一个ResultSet 结果集
        ResultSet resultSet = statement.executeQuery(sql);

        int rowCount = resultSet.next() ? resultSet.getRow() : 0;

        //6.关闭连接，释放资源
        resultSet.close();
        statement.close();
        connection.close();
        if (rowCount > 0) {
            return true;
        }
        return false;
    }


    public static void main(String[] args) throws ClassNotFoundException, SQLException {


    }

}
